package com.winning.vsts.merge;

import com.winning.vsts.merge.service.configuration.VstsContext;
import com.winning.vsts.merge.service.domain.VstsProject;
import com.winning.vsts.merge.service.service.VstsClient;
import com.winning.vsts.merge.service.service.VstsProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lps
 * @CreateTime: 2023-08-14  17:29
 * @Description: TODO
 */
@EnableJpaRepositories(basePackages = "com.winning.vsts.merge.service.jpa")
@SpringBootApplication(scanBasePackages = "com.winning.vsts.merge.service")
public class DeleteTagsApplication implements ApplicationRunner {

    private final static Logger log = LoggerFactory.getLogger(AutoMergeApplication.class);

    @Autowired
    private VstsProjectService vstsProjectService;

    @Autowired
    private VstsClient vstsClient;

    @Autowired
    private VstsContext vstsContext;


    public static void main(String[] args) {
        new SpringApplicationBuilder()
                .main(DeleteTagsApplication.class)
                .sources(DeleteTagsApplication.class)
                .web(WebApplicationType.NONE)
                .build().run(args);
    }
    @Override
    public void run(ApplicationArguments args) throws Exception {

        String tagName = "test33";
        //String tagName = "20230303北大两会";
        List<VstsProject> vstsProjects = vstsProjectService.queryVstsProject(null, null);
        log.info("===============================开启批量清理标签{}==============================",tagName);
        for (VstsProject vstsProject : vstsProjects) {
            try {
                if(!vstsProject.getProjectName().equals("winning-winex-finance")) continue;

                RestTemplate restTemplate = vstsContext.restTemplateSoa(vstsProject);
                vstsClient.DelelteTag(restTemplate, vstsProject,tagName);
                log.info("===============================项目{}标签完成===============================",vstsProject.getProjectName());
            } catch (Exception e) {
                log.warn("项目:{}/{}失败", vstsProject.getProjectOrgan(), vstsProject.getProjectName());
                log.error(e.getMessage(), e);
            }
        }
        log.info("==============================批量清理标签完成*{}*==============================",tagName);

        List<String> projectNames = new ArrayList<>();
        for (VstsProject vstsProject : vstsProjects) {
            try {
                RestTemplate restTemplate = vstsContext.restTemplate(vstsProject);
                //boolean sucess = vstsClient.checkTag(restTemplate, vstsProject,tagName);

                Boolean sucess =vstsClient.checkByTag(restTemplate,vstsProject,tagName);
                if(sucess){
                    log.info("===============================项目{}标签{}删除失败===============================",vstsProject.getProjectName(),tagName);
                    projectNames.add(vstsProject.getProjectName());
                }else{
                    log.info("===============================项目{}标签{}删除成功===============================",vstsProject.getProjectName(),tagName);

                }
            } catch (Exception e) {
                log.warn("项目:{}/{}失败", vstsProject.getProjectOrgan(), vstsProject.getProjectName());
                log.error(e.getMessage(), e);
            }
        }
        if(!projectNames.isEmpty()){
            log.info("=======================================下面是标签失败清单==================================================");
            for (String projectName : projectNames) {
                log.info("===============================项目{}标签{}处理失败，请重新执行===============================",projectName,tagName);
            }
        }
        else {
            log.info("===============================删除全部标签成功===============================");
        }
    }

}
